package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.Service.FlowerCommentService;
import com.woniu.annotation.CheckPermission;
import com.woniu.entity.FlowerComment;
import com.woniu.util.JsonResult;
import com.woniu.vo.FlowerCommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: Gwj
 * @Date: 2024/7/6 10:38
 */
@RestController
@RequestMapping("/flowerComment")
@Api(value = "鲜花评论功能", tags = "所有的鲜花评论功能")
@Slf4j
public class FlowerCommentController {
    @Resource
    private FlowerCommentService flowerCommentService;

    /**
     * 分页查询鲜花评论
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @GetMapping("/findAllFlowerComments")
    @ApiOperation(value = "查询鲜花评论的接口")
    @CheckPermission("comment:select")
    public JsonResult<PageInfo<FlowerCommentVo>> findAllFlowerComments(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "") int pageSize) throws Exception {
        PageInfo<FlowerCommentVo> allFlowerComments = flowerCommentService.findAllFlowerComments(pageNum, pageSize);
        return JsonResult.build(200, "success", allFlowerComments);
    }

    /**
     * 条件查询 并且分页
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @param startTime
     * @param endTime
     * @param nickName
     * @param orderId
     * @param flowerName
     * @return
     * @throws Exception
     */
    @GetMapping("/findAllFlowerCommentsByConditions")
    @ApiOperation(value = "根据条件查询鲜花评论的接口")
    @CheckPermission("comment:select")
    public JsonResult<PageInfo<FlowerCommentVo>> findAllFlowerCommentsByConditions(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "startTime", required = false) String startTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "orderId", required = false) String orderId,
            @RequestParam(value = "nickName", required = false) String nickName,
            @RequestParam(value = "flowerName", required = false) String flowerName
    ) throws Exception {
        FlowerCommentVo flowerCommentVo = new FlowerCommentVo();
        log.info("================================keyword=" + keyword + "startTime=" + startTime + "endTime=" + endTime +  "orderId=" + orderId +"nickName="+nickName+"flowerName="+flowerName);
        flowerCommentVo.setKeyword(keyword);
        flowerCommentVo.setStartTime(startTime);
        flowerCommentVo.setEndTime(endTime);
        flowerCommentVo.setFlowerName(flowerName);
        flowerCommentVo.setOrderId(orderId);
        flowerCommentVo.setNickName(nickName);
        PageInfo<FlowerCommentVo> allFlowerCommentsByConditions = flowerCommentService.findAllFlowerCommentsByConditions(flowerCommentVo, pageNum, pageSize);
        return JsonResult.build(200, "success", allFlowerCommentsByConditions);
    }

    @PutMapping("/deleteFlowerComment")
    @ApiOperation(value = "删除鲜花评论的接口")
    @CheckPermission("comment:delete")
    public JsonResult deleteFlowerComment(FlowerComment flowerComment) throws Exception {
        int i = flowerCommentService.changeFlowerCommentIsDelete(flowerComment);
        if (i > 0) {
            return JsonResult.build(200, "删除成功", i);
        } else {
            return JsonResult.build(500, "删除失败", i);
        }
    }

    @PutMapping("/replyFlowerComment")
    @ApiOperation(value = "恢复鲜花评论的接口")
    @CheckPermission("comment:update")
    public JsonResult replyFlowerComment(FlowerComment flowerComment) throws Exception {
        int i = flowerCommentService.replyFlowerComment(flowerComment);
        if (i > 0) {
            return JsonResult.build(200, "恢复成功", i);
        } else {
            return JsonResult.build(500, "恢复失败", i);
        }
    }
}
